/******************************************************************************
 * Author:      671643387
 * Created:     2016/9/28
 *****************************************************************************/
var mjMark = require('./mjmark.js');

//////////////////////////////////////////////////////////

/**
 * 判断第一次出牌有没有带红桃3
 * @param cards
 * @returns {boolean}
 */
function checkfristCard(playCards) {

    if(!playCards || !playCards.cards || playCards.num < 1){
        return 0;
    }
    var card = playCards.cards[0];
    if(card.type == 3 && card.num >= 1 && card.red ){
        return card.num;
    }

    return 0;
}
/**
 * 检查　玩家出的牌中是否只有一个张牌并且是 K
 * @param playCard      玩家出的牌
 * @returns {}          0 - 4
 */

function checkPokersIsK(playCards) {
    if(!playCards || !playCards.cards || playCards.num < 1){
        return 0;
    }
    if(playCards && playCards.cards && playCards.num == 1){
        if(playCards.cards[0].type == 13 && playCards.cards[0].num > 0){
            return playCards.cards[0].num;
        }
    }
    return 0;
}
/**
 * 检查　玩家出的牌中是否只有一个张牌并且是 A
 * @param playCard      玩家出的牌
 * @returns {}          0 - 4
 */
function checkPokersIsA(playCards) {
    if(playCards && playCards.cards && playCards.num == 1){
        if(playCards.cards[0].type == 1 && playCards.cards[0].num > 0){
            return playCards.cards[0].num;
        }
    }
    return 0;
}
/**
 * 检查　玩家出的牌中是否只有一个张牌并且是 2
 * @param playCard      玩家出的牌
 * @returns {}          0 - 4
 */
function checkPokersIsOne2(playCards) {
    if(playCards && playCards.cards && playCards.num == 1){
        if(playCards.cards[0].type == 2 && playCards.cards[0].num == 1){
            return true;
        }
    }
    return 0;
}
/**
 * 检查　玩家手牌中是否有相同数量的 A
 * @param playCard      玩家出的牌
 * @param numCard       数量
 * @returns {boolean}   true false
 */
function checkPlayerPokersIsOneA(handsCard, numCard) {
    numCard = numCard || 1;
    if(handsCard && handsCard.cards){
        for(var index in handsCard.cards){
            if( !handsCard.cards.hasOwnProperty(index) ){
                continue;
            }
            var iCard = handsCard.cards[index];
            if( iCard.type == 1 && iCard.num >= numCard && iCard.num != 3){
                return true;
            }
        }
    }
    return false;
}
/**
 * 检查　玩家手牌中是否有相同数量的 2
 * @param playCard      玩家出的牌
 * @param numCard       数量
 * @returns {boolean}   true false
 */
function checkPlayerPokersIsOne2(handsCard, numCard) {
    numCard = numCard || 1;
    if(handsCard && handsCard.cards){
        for(var index in handsCard.cards){
            if( !handsCard.cards.hasOwnProperty(index) ){
                continue;
            }
            var iCard = handsCard.cards[index];
            if( iCard.type == 2 && iCard.num >= numCard ){
                return true;
            }
        }
    }
    return false;
}
/**
 * 检查　玩家手牌中是否炸弹
 * @param playCard      玩家出的牌
 * @returns {boolean}   true false
 */
function checkPlayerPokersIsBomb(handsCard) {
    if(handsCard && handsCard.cards && handsCard.num > 2){
        for(var index in handsCard.cards){
            if( !handsCard.cards.hasOwnProperty(index) ){
                continue;
            }
            var iCard = handsCard.cards[index];

            if( iCard.num == 4 || (iCard.type == 1 && iCard.num == 3)){
                return true;
            }
        }
    }
    return false;
}

/**
 * 检查该牌是不是单顺
 * @param playCards      要检查的牌
 * @returns {boolean}   true false
 */
function checkIsSingleShunType(playCards) {
    // 连子小于五张不成立
    if(!playCards || playCards.num < 5){
        return false;
    }
    var cards = playCards.cards;
    var flag = true;
    var num = 0;
    var lastCard = 0;
    for (var pos in cards){
        if(!cards.hasOwnProperty(pos)){
            continue;
        }
        var iCard = cards[pos];

        // 单顺不能带 2, 并且每组牌不能大于1 张
        if(iCard.type == 2 || iCard.num > 1){
            flag = false;
            break;
        }
        if(!num){  // 第一张牌，
            ++num;
            lastCard = iCard.type;
            continue;
        }
        // 当前牌是A，转成K + 1 = 14
        var value = iCard.type;
        if(value == 1){
            value = 13 + 1;
        }
        // 上一张牌 + 1 等于当前一张牌, 每张牌只有一张
        if((lastCard + 1) != value || iCard.num != 1){
            flag = false;
            break;
        }
        lastCard = iCard.type;
    }
    return flag;
}

/**
 * 检查该牌是不是双顺
 * @param playCards      要检查的牌
 * @returns {boolean}   true false
 */
function checkIsDoubleShunType(playCards) {
    // 小于四张、不是2的倍数都不成立
    if(!playCards || playCards.num < 4 || (playCards.num % 2 != 0)){
        return false;
    }
    // 必须是两个原素以上才成立双顺
    if(playCards.cards.length < 2){
        return false;
    }
    var cards = playCards.cards;
    var flag = true;
    var num = 0;
    var lastCard = 0;
    for (var pos in cards){
        if(!cards.hasOwnProperty(pos)){
            continue;
        }
        var iCard = cards[pos];
        // 双顺不能带 2， 并且每组牌不能大于两张
        if(iCard.type == 2 || iCard.num > 2){
            flag = false;
            break;
        }
        if(!num){  // 第一张牌，
            ++num;
            lastCard = iCard.type;
            continue;
        }
        // 当前牌是A，转成K + 1 = 14
        var value = iCard.type;
        if(value == 1){
            value = 13 + 1;
        }
        // 上一张牌 + 1 等于当前一张牌  当前牌只有是两张
        if((lastCard + 1) != value|| iCard.num != 2){
            flag = false;
            break;
        }
        lastCard = iCard.type;
    }
    return flag;
}

/**
 * 检查该牌是不是 三顺
 * @param playCards      要检查的牌
 * @returns {boolean}   true false
 */
function checkIsThreeShunBandType(playCards) {
    var flag = false;
    if(!playCards && !playCards.cards && playCards.num < 8){
        return flag;
    }
    var num = 0;
    var numThreeCard = 0;
    var numSingleCard = 0;
    var numDoubleCard = 0;
    var lastThreeCard = {};
    for(var pos in playCards.cards){
        if( !playCards.cards.hasOwnProperty(pos)){
            continue;
        }
        var iCard = playCards.cards[pos];
        //  飞机带的牌不能是2，        A 不能三张当飞机出
        if( iCard.type == 2 || (iCard.type == 1 && iCard.num == 3) ){
            numThreeCard = 0;
            numSingleCard = 0;
            numDoubleCard = 0;
            break;
        }
        // 保存上一个三带的字面值
        if(iCard.num == 3 && !num ){
            ++num;
            ++numThreeCard;
            lastThreeCard = iCard;
            continue;
        }
        // 三个A不能当飞机， 2 不能当飞机， 当飞机的牌必须是三张一样的
        if(iCard.num == 3){
            // 上一个三带的字面值加一 ！= 当前三带牌的字面值者飞机不成立
            if(lastThreeCard.type + 1 != iCard.type){
                numThreeCard = 0;
                numSingleCard = 0;
                numDoubleCard = 0;
                break;
            }
            lastThreeCard = iCard;
            ++numThreeCard;
        }else if( iCard.num == 1){    // 一个飞机只能带一组牌，带的一组带可以是 单张
            ++numSingleCard;
        }else if (iCard.num == 2){   //  一个飞机只能带一组牌，带的一组带可以是 双张
            ++numDoubleCard;
        }else{  // 这些条件都不是，者飞机不成行
            numThreeCard = 0;
            numSingleCard = 0;
            numDoubleCard = 0;
            break;
        }
    }

    // 三张的个数 大于 1，   飞机带的牌必须是 单 或者 双 一种类型的，
    if(numThreeCard > 1 && ((numThreeCard == numSingleCard && numDoubleCard == 0) || (numThreeCard == numDoubleCard && numSingleCard == 0))){
        flag = true;
    }
    return flag;
}
/**
 * 检查该牌是不是 四带两手
 * @param playCards      要检查的牌
 * @returns {boolean}   true false
 */
function checkIsFourBandOneType(playCards) {
    // 小于六张不成立，大于八张不成立, 不是2的位数单张不立
    if(!playCards || playCards.num < 6 || playCards.num > 8 || playCards.num % 2 != 0){
        return false;
    }
    // 必须是三组
    if(playCards.cards.length != 3){
        return false;
    }
    var flag = false;
    var cards = playCards.cards;
    var otherType = 0;
    var otherNum = 0;
    var index = 0;
    for (var pos in cards){
        if(!cards.hasOwnProperty(pos)){
            continue;
        }
        var iCard = cards[pos];
        // 四带一手不能带2, 并且每组牌不能大于四张， 不能带三张一样的
        if(iCard.type == 2 || iCard.num > 4 || iCard.num == 3){
            flag = false;
            break;
        }
        //第一组牌
        if(index == 0 && iCard.num < 4) {
            otherType = iCard.type;
            otherNum = iCard.num;
            ++index;
            continue;
        }
        // 其它的牌，          字面值要不相等，               牌的数量相等,         并只有带小于等于两张的牌
        if(iCard.num != 4 && (iCard.type == otherType || iCard.num != otherNum || iCard.num > 2)) {
            flag = false;
            break;
        }
        // 有一组牌四张， 并且没有还没有出现过四张的
        if(iCard.num == 4 && !flag){
            flag = true;
            continue;
        }
        // 已经出现过四张的了，
        if(iCard.num == 4){
            flag = false;
            break;
        }
    }
    return flag;
}

/**
 * 检查该牌是不是  三带一手
 * @param playCards      要检查的牌
 * @returns {boolean}   true false
 */
function checkIsThreeBandOneType(playCards) {
    // 小于四张不成立，大于五张不成立,
    if(!playCards || playCards.num < 4 || playCards.num > 5){
        return false;
    }
    // 必须是 两组
    if(playCards.cards.length != 2){
        return false;
    }
    var flag = false;
    var cards = playCards.cards;
    var isOtherCard = false;
    var isNormalCard = false;
    for (var pos in cards){
        if(!cards.hasOwnProperty(pos)){
            continue;
        }
        var iCard = cards[pos];
        // 三带一手不能带2, 每组牌不能大于三张,  捉老麻子游戏中， A不能用做三带一手
        if(iCard.type == 2 || iCard.num > 3 || (iCard.type == 1 && iCard.num == 3)){
            flag = false;
            break;
        }
        // 有正常的三张牌并且也有带的一手，如果还循环表示牌多了
        if(isOtherCard && isNormalCard){
            flag = false;
            break;
        }
        //第一组牌
        if(iCard.num < 3 && iCard.num > 0) {
            isOtherCard = true;
            continue;
        }
        // 有一组牌三张， 并且没有还没有出现过三张的
        if(iCard.num == 3 && !flag){
            isNormalCard = true;
            flag = true;
            continue;
        }
        // 已经出现过三张的了，
        if(iCard.num == 3){
            flag = false;
            break;
        }
    }
    return flag;
}

/**
 * 检查该牌是不是 炸弹
 * @param playCards      要检查的牌
 * @returns {boolean}   true false
 */
function checkIsBombType(playCards) {
    // 大于四张不成立
    if(!playCards || playCards.num > 4){
        return false;
    }
    // 必须是 一组
    if(playCards.cards.length != 1){
        return false;
    }
    var flag = false;
    var cards = playCards.cards;
    var isAppearBomb = false;
    for (var pos in cards){
        if(!cards.hasOwnProperty(pos)){
            continue;
        }
        var iCard = cards[pos];

        // 每组牌不能大于四张，捉老麻子中炸弹不能有2， 三个A也是炸弹
        if(iCard.type == 2 || iCard.num > 4){
            flag = false;
            break;
        }
        // 已经出现过炸弹了还有循环，牌出多了
        if(isAppearBomb){
            flag = false;
            break
        }
        //一组炸弹是四张牌，  在捉老麻子中游戏中，三个A也是炸弹
        if((iCard.num == 4 && iCard.type != 1) ||  (iCard.type == 1 && iCard.num == 3)) {
            isAppearBomb = true;
            flag = true;
            continue;
        }
    }
    return flag;
}

/**
 * 检查该牌是不是 对子
 * @param playCards      要检查的牌
 * @returns {boolean}   true false
 */
function checkIsDoubleSubType(playCards) {
    // 不等于两张不成立
    if(!playCards || playCards.num != 2){
        return false;
    }
    // 必须是 一组
    if(playCards.cards.length != 1){
        return false;
    }
    var flag = false;
    var cards = playCards.cards;
    var isFrist = false;
    for (var pos in cards){
        if(!cards.hasOwnProperty(pos)){
            continue;
        }
        var iCard = cards[pos];
        // 已经出现过一对了还有循环，牌出多了
        if(isFrist){
            flag = false;
            break
        }
        isFrist = true;
        // 每组牌不能大于两张
        if(iCard.num == 2){
            flag = true;
        }
    }
    return flag;
}

/**
 * 检查该牌是不是 单张
 * @param playCards      要检查的牌
 * @returns {boolean}   true false
 */
function checkIsSingleCardType(playCards) {
    // 不等于两张不成立
    if(!playCards || playCards.num != 1){
        return false;
    }
    // 必须是 一组
    if(playCards.cards.length != 1){
        return false;
    }
    var flag = false;
    var cards = playCards.cards;
    var isFrist = false;
    for (var pos in cards){
        if(!cards.hasOwnProperty(pos)){
            continue;
        }
        var iCard = cards[pos];
        // 已经出现过一对了还有循环，牌出多了
        if(isFrist){
            flag = false;
            break
        }
        isFrist = true;
        // 每组牌不能大于两张
        if(iCard.num == 1){
            flag = true;
        }
    }
    return flag;
}
///////////////////////////////////////////////////////////////////////
/**
 * 检查当前 单顺子、双顺子 能否吃得起上一家出的 单顺子、双顺子
 * @param newCards
 * @param oldCards
 */
function checkIsEatShun(newCards, oldCards) {
    if(!newCards || !oldCards || newCards.num != oldCards.num){
        return false;
    }
    var cards1 = {};
    var cards2 = {};

    var flag = false;

    for (var pos in newCards.cards){
        if(!newCards.cards.hasOwnProperty(pos)){
            continue;
        }
        cards1 = newCards.cards[pos]; // 取出当前牌组的第一张
        break;
    }
    for (var pos in oldCards.cards){
        if(!oldCards.cards.hasOwnProperty(pos)){
            continue;
        }
        cards2 = oldCards.cards[pos]; // 取出上一家出的牌组的第一张
        break;
    }
    // 前牌组的第一张 大于 上一家出的牌组的第一张，
    if(cards1.type > cards2.type && cards1.num == cards2.num){
        flag = true;
    }
    return flag;
}

/**
 * 检查当前 三顺 能否吃得起上一家出的 三顺
 * @param newCards
 * @param oldCards
 */
function checkIsEatThreeShun(newCards, oldCards) {
    var flag = false;
    if(!newCards || !oldCards || newCards.num != oldCards.num){
        return flag;
    }
    var newValue = 0;
    var oldValue = 0;
    for(var pos in newCards.cards){
        if( !newCards.cards.hasOwnProperty(pos)){
            continue;
        }
        // 找出newCards的第一个三张牌
        if( newCards.cards[pos].num == 3){
            newValue = newCards.cards[pos].type;
        }
    }
    for(var pos in oldCards.cards){
        if( !oldCards.cards.hasOwnProperty(pos)){
            continue;
        }
        // 找出oldCards的第一个三张牌
        if( oldCards.cards[pos].num == 3){
            oldValue = oldCards.cards[pos].type;
        }
    }
    if(newValue > oldValue){
        flag = true;
    }
    return flag;
}
/**
 * 检查当前 四带两手 能否吃得起上一家出的 四带两手
 * @param newCards
 * @param oldCards
 */
function checkIsEatFourBandTwo(newCards, oldCards) {
    if(!newCards || !oldCards || newCards.num != oldCards.num){
        return false;
    }
    var cards1 = {};
    var cards2 = {};
    var flag = false;
    for (var pos in newCards.cards){
        if(!newCards.cards.hasOwnProperty(pos)){
            continue;
        }
        var iCard = newCards.cards[pos];
        if(iCard.num == 4){ // 取出当前牌组中的有三张的
            cards1 = iCard;
            break;
        }
    }
    for (var pos in oldCards.cards){
        if(!oldCards.cards.hasOwnProperty(pos)){
            continue;
        }
        var iCard = oldCards.cards[pos];
        if(iCard.num == 4){ // 取出当前牌组中的有三张的
            cards2 = iCard;
            break;
        }
    }
    // 当前牌组的四张 大于 上一家出的牌组的四张，
    if(cards1 && cards2 && cards1.type > cards2.type){
        flag = true;
    }
    return flag;
}
/**
 * 检查当前 三带一手 能否吃得起上一家出的 三带一手
 * @param newCards
 * @param oldCards
 */
function checkIsEatThreeBandOne(newCards, oldCards) {
    if(!newCards || !oldCards || newCards.num != oldCards.num){
        return false;
    }
    var cards1 = {};
    var cards2 = {};

    var flag = false;

    for (var pos in newCards.cards){
        if(!newCards.cards.hasOwnProperty(pos)){
            continue;
        }
        var iCard = newCards.cards[pos];
        if(iCard.num == 3){ // 取出当前牌组中的有三张的
            cards1 = iCard;
            break;
        }
    }
    for (var pos in oldCards.cards){
        if(!oldCards.cards.hasOwnProperty(pos)){
            continue;
        }
        var iCard = oldCards.cards[pos];
        if(iCard.num == 3){     // 取出上一家牌组中有三张的
            cards2 = iCard;
            break;
        }
    }
    // 当前牌组中的三张 大于 上一家牌组中的三张
    if(cards1 && cards2 && cards1.type > cards2.type){
        flag = true;
    }
    return flag;
}

/**
 * 检查当前 炸弹 能否吃得起上一家出的 炸弹
 * @param newCards
 * @param oldCards
 */
function checkIsEatBomb(newCards, oldCards) {
    if(!newCards || !oldCards){
        return false;
    }
    var cards1 = {};
    var cards2 = {};
    var flag = false;

    for (var pos in newCards.cards){
        if(!newCards.cards.hasOwnProperty(pos)){
            continue;
        }
        var iCard = newCards.cards[pos];
        if(iCard.num == 4 ||  iCard.type == 1 && iCard.num == 3){ // 取出当前牌组中的有四张的 或者 三个A
            cards1 = iCard;
            break;
        }
    }
    for (var pos in oldCards.cards){
        if(!oldCards.cards.hasOwnProperty(pos)){
            continue;
        }
        var iCard = oldCards.cards[pos];
        if(iCard.num == 4 ||  (iCard.type == 1 && iCard.num == 3)){     // 取出上一家牌组中有四张的 或者 三个A
            cards2 = iCard;
            break;
        }
    }
    if(cards1 && cards2 ) {
        var newValue = cards1.type == 1 ? 13 + 1 : cards1.type;
        var oldValue = cards2.type == 1 ? 13 + 1 : cards2.type;

        // 上家的炸弹是天炸没有牌可以炸它
        if( oldValue == 3){
            flag = false;
        }else if (newValue > oldValue) {// 当前牌组中的四张 大于 上一家牌组中的四张
            flag = true;
        }
    }
    return flag;
}

/**
 * 检查当前 一对、一张 能否吃得起上一家出的 一对、一张
 * @param newCards
 * @param oldCards
 */
function checkIsEatSingleOrDouble(newCards, oldCards) {
    if(!newCards || !oldCards || newCards.length != oldCards.length){
        return false;
    }
    var cards1 = {};
    var cards2 = {};
    for (var pos in newCards.cards) {
        if (!newCards.cards.hasOwnProperty(pos)) {
            continue;
        }
        cards1 = newCards.cards[pos];
        break;
    }
    for (var pos in oldCards.cards) {
        if (!oldCards.cards.hasOwnProperty(pos)) {
            continue;
        }
        cards2 = oldCards.cards[pos];
        break;
    }
    if(cards1 && cards2 ) {
        var newValue = cards1.type == 1 ? 13 + 1 : (cards1.type == 2 ? 13 + 2 : cards1.type);
        var oldValue = cards2.type == 1 ? 13 + 1 : (cards2.type == 2 ? 13 + 2 : cards2.type);
        if (newValue > oldValue && cards1.num == cards2.num) {
            return true;
        }
    }
    return false;
}

/**
 * 检查牌的类型
 * @param cards
 */
function checkCardType(flag, playCards) {

    var mark = 0;
    if( !playCards || playCards.num < 1){
        return mark;
    }
    if(playCards.num >= 5 && checkIsSingleShunType(playCards)){                                                                // 单顺
        mark = mjMark.PLAY_CARD_TYPE_SINGLE_SHUN;
    }else if(playCards.num >= 4 && playCards.num % 2 == 0 && checkIsDoubleShunType(playCards)){                               // 双顺
        mark = mjMark.PLAY_CARD_TYPE_DOUBLE_SHUN;
    }else if(playCards.num >= 8 && checkIsThreeShunBandType(playCards)){                                                     // 三顺带一手
        mark = mjMark.PLAY_CARD_TYPE_THREE_SHUN_BAND_ONE;
    }else if(playCards.num > 5 && playCards.num % 2 == 0 && checkIsFourBandOneType(playCards)) {                            // 四带两手
        mark = mjMark.PLAY_CARD_TYPE_FOUR_BAND_TWO;
    }else if(playCards.num > 3 && playCards.num < 6 && checkIsThreeBandOneType(playCards)) {                                // 三带一手
        mark = mjMark.PLAY_CARD_TYPE_THREE_BAND_ONE;
    }else if(playCards.num > 2 && playCards.num < 5 && checkIsBombType(playCards)){                                       // 炸弹     四个一样的炸弹，或者三个A 最大的炸弹
        mark = mjMark.PLAY_CARD_TYPE_BOMB;
    }else if(playCards.num == 3 && flag && playCards.cards.length == 1){             // 三不代
        mark = mjMark.PLAY_CARD_TYPE_SANBUDAI;
    }else if(playCards.num == 2 && checkIsDoubleSubType(playCards)){                                                     // 对子
        mark = mjMark.PLAY_CARD_TYPE_DOUBLE_SUB;
    }else if(playCards.num == 1 && checkIsSingleCardType(playCards)){                                                   // 单张
        mark = mjMark.PLAY_CARD_TYPE_SINGLE;
    }
    return mark;
}

/**
 * 检查能否吃牌
 * @param newCards  当前出牌
  * @param oldCards  上一家牌
 * @returns {boolean}   true ，false
 */
function checkIsCanEatCard(newCards, oldCards) {
    var flag = false;
    if(!newCards || !oldCards){
        return flag;
    }
    var oldCardType = checkCardType(false, oldCards);
    var newCardType = checkCardType(false, newCards);
    //当前出的牌是炸弹，上家的牌不是炸弹
    if(newCardType == mjMark.PLAY_CARD_TYPE_BOMB && oldCardType != mjMark.PLAY_CARD_TYPE_BOMB){
        return true;
    }
    // 牌型不一样，又没有炸弹，不能吃
    if(oldCardType != newCardType){
        return false;
    }
    // 牌型一样，每个牌型对比
    switch (oldCardType){
        case mjMark.PLAY_CARD_TYPE_SINGLE_SHUN:// 单顺
        case mjMark.PLAY_CARD_TYPE_DOUBLE_SHUN:// 双顺
            flag = checkIsEatShun(newCards, oldCards);
            break;
        case mjMark.PLAY_CARD_TYPE_THREE_SHUN_BAND_ONE:// 三顺带一手
            flag = checkIsEatThreeShun(newCards, oldCards);
            break;
        case mjMark.PLAY_CARD_TYPE_FOUR_BAND_TWO:// 四带两手
            flag = checkIsEatFourBandTwo(newCards, oldCards);
            break;
        case mjMark.PLAY_CARD_TYPE_THREE_BAND_ONE:// 三带一手
            flag = checkIsEatThreeBandOne(newCards, oldCards);
            break;
        case mjMark.PLAY_CARD_TYPE_BOMB:        // 炸弹
            flag = checkIsEatBomb(newCards, oldCards);
            break;
        case mjMark.PLAY_CARD_TYPE_DOUBLE_SUB: // 对子
        case mjMark.PLAY_CARD_TYPE_SINGLE:      // 单张
            flag = checkIsEatSingleOrDouble(newCards, oldCards);
            break;
        default:
            break;
    }
    return flag;
}

/**
 * 检查玩家是否作弊
 * @param roomPlayerArray       当前房间所有玩家数组
 * @param curUid                当前出牌玩家Uid
 * @param playCardType          当前出牌的类型
 */
function checkPlayerIsDischargeWater(curPlayer, nextPlayer, playCard, playCardType ){

    var flag = false;
    // 先每次到该玩家出牌， 先清作弊标志
    curPlayer.setPlayerIsDischargeWater( flag );
    if((playCardType != mjMark.PLAY_CARD_TYPE_DOUBLE_SUB) && (playCardType != mjMark.PLAY_CARD_TYPE_SINGLE)){
        return;
    }
    var baoPaiNum = 0;

    baoPaiNum = nextPlayer.handsPokers.getRemainCardNum();

    // 检查出的牌是不是对子，                               并且有玩家报双
    if(playCardType == mjMark.PLAY_CARD_TYPE_DOUBLE_SUB && curPlayer.handsPokers.getRemainCardNum() > 2 && baoPaiNum == 2) {
        // 有玩家报双，当前出的牌是双张，检查是否是玩家手中最大的牌，如果不是者作弊
        var cardType = playCard.cards[0].type;
        // 遍历玩家手牌
        for (var index in curPlayer.handsPokers.cards){
            if(!curPlayer.handsPokers.cards.hasOwnProperty(index)){
                continue;
            }
            var iCard = curPlayer.handsPokers.cards[index];
            // 手牌中有大于当前出的牌
            if(iCard.num > 1 && iCard.type > cardType){
                flag = true;
                break;
            }
        }
    }else if(playCardType == mjMark.PLAY_CARD_TYPE_SINGLE && curPlayer.handsPokers.getRemainCardNum() > 1 && baoPaiNum == 1){
        // 有玩家报单，当前出的牌是单张，检查是否是玩家手中最大的牌，如果不是者作弊
        var cardType = playCard.cards[0].type;
        // 遍历玩家手牌
        for (var index in curPlayer.handsPokers.cards){
            if(!curPlayer.handsPokers.cards.hasOwnProperty(index)){
                continue;
            }
            var iCard = curPlayer.handsPokers.cards[index];
            // 手牌中有大于当前出的牌
            if(iCard.num > 0 && iCard.type > cardType){
                flag = true;
                break;
            }
        }
    }
    if(flag) {
        curPlayer.setPlayerIsDischargeWater( flag );
    }
}

/**
 * 玩家Pass时检查是否作弊
 * @param roomPlayerArray       当前房间所有玩家数组
 * @param curUid                当前出牌玩家Uid
 * @param playCardType          当前出牌的类型
 */
function checkPassIsDischargeWater(curPlayer, nextPlayer, playCard, playCardType ){
    if((playCardType != mjMark.PLAY_CARD_TYPE_DOUBLE_SUB) && (playCardType != mjMark.PLAY_CARD_TYPE_SINGLE)){
        return;
    }
    var baoPaiNum = 0;
    var flag = false;

    baoPaiNum = nextPlayer.handsPokers.getRemainCardNum();

    // 检查出的牌是不是对子，                               并且有玩家报双
    if(playCardType == mjMark.PLAY_CARD_TYPE_DOUBLE_SUB && baoPaiNum == 2 ) {
        var cardType = playCard.cards[0].type;
        cardType = cardType == 1 ? 13 + 1 : (cardType == 2 ? 13 + 2 : cardType);
        for (var index in curPlayer.handsPokers.cards){
            if(!curPlayer.handsPokers.cards.hasOwnProperty(index)){
                continue;
            }
            var iCard = curPlayer.handsPokers.cards[index];
            var newCardType = iCard.type;
            newCardType = newCardType == 1 ? 13 + 1 : (newCardType == 2 ? 13 + 2 : newCardType);
            // 手牌中有大于当前出的牌
            if(iCard.num > 1 && newCardType > cardType){
                curPlayer.setPlayerIsDischargeWater( true );
                return;
            }
        }
    }else if(playCardType == mjMark.PLAY_CARD_TYPE_SINGLE && baoPaiNum == 1){
        // 有玩家报单，当前出的牌是单张，检查是否是玩家手中最大的牌，如果不是者作弊
        var cardType = playCard.cards[0].type;
        cardType = cardType == 1 ? 13 + 1 : (cardType == 2 ? 13 + 2 : cardType);
        // 遍历玩家手牌
        for (var index in curPlayer.handsPokers.cards){
            if(!curPlayer.handsPokers.cards.hasOwnProperty(index)){
                continue;
            }
            var iCard = curPlayer.handsPokers.cards[index];
            var newCardType = iCard.type;
            newCardType = newCardType == 1 ? 13 + 1 : (newCardType == 2 ? 13 + 2 : newCardType);
            // 手牌中有大于当前出的牌
            if(iCard.num > 0 && newCardType > cardType){
                curPlayer.setPlayerIsDischargeWater( true );
                return;
            }
        }
    }
}
exports.checkfristCard = checkfristCard;
exports.checkCardType = checkCardType;
exports.checkIsCanEatCard = checkIsCanEatCard;
exports.checkPokersIsK = checkPokersIsK;
exports.checkPokersIsA = checkPokersIsA;
exports.checkPokersIsOne2 = checkPokersIsOne2;
exports.checkPlayerPokersIsOneA = checkPlayerPokersIsOneA;
exports.checkPlayerPokersIsOne2 = checkPlayerPokersIsOne2;
exports.checkPlayerPokersIsBomb = checkPlayerPokersIsBomb;
exports.checkPlayerIsDischargeWater = checkPlayerIsDischargeWater;
exports.checkPassIsDischargeWater = checkPassIsDischargeWater;


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 测试胡牌算法使用

function HandCards() {
    this.num = 0;           // 当前牌总数
    this.cards = [];        // 玩家手牌

    //this.init();
}

var handCards = new HandCards();
handCards.num = 2;
handCards.cards = [ {"type":  5, "num": 1, "red": 1, "black": 0, "block": 0, "plum": 0,},
                    {"type": 6, "num": 3, "red": 1, "black": 0, "block": 0, "plum": 0,},
                    {"type": 7, "num": 3, "red": 1, "black": 0, "block": 0, "plum": 0,},
                    {"type": 8, "num": 3, "red": 1, "black": 0, "block": 0, "plum": 0,},
                    {"type": 9, "num": 1, "red": 1, "black": 0, "block": 0, "plum": 0,},
                    {"type":  11, "num": 1, "red": 1, "black": 0, "block": 0, "plum": 0,},/**/
                ];
var newCard = new HandCards();
newCard.num = 2;
newCard.cards = [   {"type":  1, "num": 2, "red": 1, "black": 0, "block": 0, "plum": 0,},
                    {"type": 10, "num": 2, "red": 1, "black": 0, "block": 0, "plum": 0,},
                    {"type": 11, "num": 2, "red": 1, "black": 0, "block": 0, "plum": 0,},
                    {"type": 12, "num": 2, "red": 1, "black": 0, "block": 0, "plum": 0,},
                    {"type": 13, "num": 2, "red": 1, "black": 0, "block": 0, "plum": 0,},
                    {"type":  1, "num": 2, "red": 1, "black": 0, "block": 0, "plum": 0,},
                ];
/*console.log("is red 3 = "+ fristCard(handCards));
console.log("is single shun = " + checkIsSingleShun(handCards));
console.log("is double shun = " + checkIsDoubleShun(handCards));
console.log("is four band one = " + checkIsFourBandOne(handCards));
console.log("is three band one = " + checkIsThreeBandOne(handCards));
console.log("is bomb = " + checkIsBomb(handCards));
console.log("is double = " + checkIsDoubleSub(handCards));
console.log("is single = " + checkIsSingleCard(handCards));
console.log("is = " + checkIsCanEatCard(newCard , handCards));
console.log("is = " + checkIsThreeShunBandType(handCards));*/


/**
 * 提示手中有没有牌可以吃
 * @param playerHandsPokers 玩家手牌
 * @param playCards         要压的牌
 * @param clickNum          点过几次提示
 * @returns {boolean}
 */
/*

function checkHandsPokersIsEatCard ( playerHandsPokers, playCards, clickNum ) {
    var eatCards = {
        cards : [],
        num : 0
    };
    if( !playerHandsPokers || !playCards ){
        return eatCards;
    }
    var playCardType = checkCardType(playCards);
    /!*if(checkHandsPokersIsCardType( playerHandsPokers, playCardType)){
     }*!/
    // 检查一张
    if(mjMark.PLAY_CARD_TYPE_SINGLE == playCardType){
        var singleCard = 0;
        // 找到上家出牌牌型成立中最小值
        for(var pos in playCards.cards){
            if(!playCards.cards.hasOwnProperty(pos)) {continue;}
            singleCard = playCards.cards[pos].type;
            break;
        }
        // 如果是A 转化成 14，2转化成15
        singleCard = singleCard == 1 ? 13 + 1 : (singleCard == 2 ? 13 + 2 : singleCard);
        var number = 0;
        // 遍历玩家手牌
        for(var index in playerHandsPokers.cards){
            if( !playerHandsPokers.cards.hasOwnProperty(index) ){continue;}
            var iCard = playerHandsPokers.cards[index];

            var iCardType = iCard.type == 1 ? 13 + 1 : (iCard.type == 2 ? 13 + 2 : iCard.type);
            // 玩家手牌中，当前牌大于上家出的牌， 或者有炸弹
            if( iCardType > singleCard || iCard.num == 4 || (iCard.type == 1 && iCard.num == 3)){
                // 玩家点了几点提示，跳过几次
                if( clickNum > number) {
                    ++number;
                    continue;
                }else{
                    var cNum = (iCard.type == 1 && iCard.num == 3) ? 3 : (iCard.num == 4 ? 4 : 1);
                    eatCards.cards.push({type:iCard.type, num : cNum});
                    eatCards.num = 1;
                    break;
                }
            }
        }
    }else if(mjMark.PLAY_CARD_TYPE_DOUBLE_SUB == playCardType){
        // 检查一对
        var doubleCard = 0;
        // 找到上家出牌牌型成立中最小值
        for(var pos in playCards.cards){
            if(!playCards.cards.hasOwnProperty(pos)) {continue;}
            doubleCard = playCards.cards[pos].type;
            break;
        }
        // 如果是A 转化成 14，2转化成15
        doubleCard = doubleCard == 1 ? 13 + 1 : (doubleCard == 2 ? 13 + 2 : doubleCard);
        var number = 0;
        // 遍历玩家手牌
        for(var index in playerHandsPokers.cards){
            if( !playerHandsPokers.cards.hasOwnProperty(index) ){continue;}
            var iCard = playerHandsPokers.cards[index];

            var iCardType = iCard.type == 1 ? 13 + 1 : (iCard.type == 2 ? 13 + 2 : iCard.type);
            // 玩家手牌中，当前牌大于上家出的牌， 或者有炸弹
            if( (iCardType > doubleCard && iCard.num == 2) || iCard.num == 4 || (iCard.type == 1 && iCard.num == 3)){
                // 玩家点了几点提示，跳过几次
                if( clickNum > number) {
                    ++number;
                    continue;
                }else{
                    var cNum = (iCard.type == 1 && iCard.num == 3 ) ? 3 : (iCard.num == 4 ? 4 : 2);
                    eatCards.cards.push({type:iCard.type, num : cNum});
                    eatCards.num = 1;
                    break;
                }
            }
        }
    }else if (mjMark.PLAY_CARD_TYPE_BOMB == playCardType){
        // 炸弹
        var bombCard = 0;
        // 找到上家出牌牌型成立中最小值
        for(var pos in playCards.cards){
            if(!playCards.cards.hasOwnProperty(pos)) {continue;}
            bombCard = playCards.cards[pos].type;
            break;
        }
        // 如果是A 转化成 14，2转化成15
        bombCard = bombCard == 1 ? 13 + 1 : (bombCard == 2 ? 13 + 2 : bombCard);
        // 遍历玩家手牌
        // 上家出的是天炸
        if(bombCard == 3){
            eatCards.cards = [];
            eatCards.num = 0;
            return eatCards;
        }
        // 遍历玩家手牌
        for(var index in playerHandsPokers.cards){
            if( !playerHandsPokers.cards.hasOwnProperty(index) ){continue;}
            var iCard = playerHandsPokers.cards[index];
            var iCardType = iCard.type == 1 ? 13 + 1 : (iCard.type == 2 ? 13 + 2 : iCard.type);
            // 玩家手牌中，当前牌等于四张 ，或者 三个A，大于上家出的牌
            if( iCardType > bombCard && (iCard.num == 4 || (iCard.type == 1 && iCard.num == 3))){
                // 玩家点了几点提示，跳过几次
                if( clickNum > number) {
                    ++number;
                    continue;
                }else{
                    var cNum = (iCard.type == 1 && iCard.num == 3 ) ? 3 : iCard.num;
                    eatCards.cards.push({type:iCard.type, num : cNum});
                    eatCards.num = 1;
                }
            }
        }
    }else if (mjMark.PLAY_CARD_TYPE_THREE_BAND_ONE == playCardType){
        // 三带一手
        var threeCard = 0;
        var beltCardNum = 0;
        // 找到上家出牌牌型成立中最小值
        for(var pos in playCards.cards){
            if(!playCards.cards.hasOwnProperty(pos)) {continue;}
            if( playCards.cards[ pos ].num == 3) {
                // 找到三张一样的牌
                threeCard = playCards.cards[ pos ].type;
            }else{
                // 每手副牌的数量
                beltCardNum = playCards.cards[pos].num;
            }
        }
        var isBeltCard = false;
        var isThreeCard = false;
        // 遍历玩家手牌
        for(var index in playerHandsPokers.cards){
            if( !playerHandsPokers.cards.hasOwnProperty(index) ){continue;}
            var iCard = playerHandsPokers.cards[index];
            // 玩家手牌中，当前牌张数是三张，或者是四张 或者是三个A
            if( (iCardType > threeCard && iCard.num == 3) || iCard.num == 4 || (iCard.type == 1 && iCard.num == 3)){
                // 玩家点了几点提示，跳过几次
                if( clickNum > number || isThreeCard) {
                    ++number;
                    continue;
                }else{
                    var cNum = (iCard.type == 1 && iCard.num == 3 ) ? 3 : (iCard.num == 4 ? 4 : iCard.num);
                    eatCards.cards.push({type:iCard.type, num : cNum});
                    eatCards.num = 1;
                    if((iCard.type == 1 && iCard.num == 3 ) || iCard.num == 4){
                        return eatCards;
                    }
                    isThreeCard = true;
                }
            }else if (iCard.num == beltCardNum && iCard.type != 2){ // 三带一中 带的牌
                // 带的一手牌的中不能带 2

                // 前面已经找到带的牌了
                if (isBeltCard){
                    continue;
                }
                eatCards.cards.push({type:iCard.type, num : beltCardNum});
                eatCards.num += 1;
                isBeltCard = true;
                // 找到 三带一中的三张一样的牌了
                if(isThreeCard > 0){
                    break;
                }
            }
        }
        // 没有找到带的牌或者 三张一样的牌
        if(!isBeltCard || !isThreeCard){
            eatCards.cards = [];
            eatCards.num = 0;
        }
    }else if (mjMark.PLAY_CARD_TYPE_FOUR_BAND_TWO == playCardType){
        // 四带两手
        var fourCard = 0;
        var beltCardNum = 0;
        // 找到上家出牌牌型成立中最小值
        for(var pos in playCards.cards){
            if(!playCards.cards.hasOwnProperty(pos)) {continue;}
            if( playCards.cards[ pos ].num == 4) {
                // 找到四张一样的牌
                fourCard = playCards.cards[ pos ].type;
            }else{
                // 每手副牌的数量
                beltCardNum = playCards.cards[pos].num;
            }
        }
        // 遍历玩家手牌， 有炸弹就吃，不提示四带二了
        for(var index in playerHandsPokers.cards) {
            if ( !playerHandsPokers.cards.hasOwnProperty( index ) ) {
                continue;
            }

            var iCard = playerHandsPokers.cards[ index ];
            // 当前牌有四张并且 比上家的四带二中的正牌大 就提示四 带二
            if ( iCard.num == 4 || (iCard.type == 1 && iCard.num == 3) ) {
                if ( clickNum > number ) {
                    ++number;
                    continue;
                } else {
                    var cNum = (iCard.type == 1 && iCard.num == 3 ) ? 3 : (iCard.num == 4 ? 4 : iCard.num);
                    eatCards.cards.push( { type : iCard.type, num : cNum } );
                    eatCards.num = 1;
                }
            }
        }
    }else if (mjMark.PLAY_CARD_TYPE_THREE_SHUN_BAND_ONE == playCardType){
        // 三顺带一手
        var threeShunCard = 0;
        var threeShunNum = 0;
        var beltCardNum = 0;
        // 找到三顺中最大的正牌，几个正牌，和带每手副牌的数量
        for(var pos in playCards.cards){
            if(!playCards.cards.hasOwnProperty(pos)) {continue;}
            if( playCards.cards[ pos ].num == 3) {
                threeShunCard = playCards.cards[ pos ].type;
                ++threeShunNum;
            }else{
                beltCardNum = playCards.cards[pos].num;
            }
        }
        var findBeltCardNum = 0;
        var lastThreeShunCard = 0;
        var findThreeShunNum = 0;
        var isUseBomb = false;
        // 遍历手牌
        for(var index in playerHandsPokers.cards){
            if( !playerHandsPokers.cards.hasOwnProperty(index) ){continue;}
            var iCard = playerHandsPokers.cards[index];

            if(iCard.type == 2){
                continue;
            }
            //找到三张一样的牌，比较
            if( iCard.type > threeShunCard && iCard.num == 3){
                if(!lastThreeShunCard){
                    lastThreeShunCard = iCard.type;
                    ++findThreeShunNum;
                    continue;
                }
                //三张一样的，连不上,清空重新找
                if(lastThreeShunCard + 1 != iCard.type){
                    lastThreeShunCard = 0;
                    findThreeShunNum = 0;
                    continue;
                }
                ++findThreeShunNum;
                lastThreeShunCard = iCard.type;

                eatCards.cards.push({type:iCard.type, num : iCard.num});
                eatCards.num += 1;

                if(findThreeShunNum == threeShunNum){
                    continue;
                }
            }else if(iCard.num == 4 || (iCard.type == 1 && iCard.num == 3)){
                //使用炸弹
                eatCards.cards.push({type: iCard.type, num : iCard.num});
                eatCards.num = 1;
                isUseBomb = true;
                break;
            }else if (iCard.num == beltCardNum && iCard.type != 2){
                eatCards.cards.push({type:iCard.type, num : iCard.num});
                eatCards.num += 1;
                ++findBeltCardNum;
            }
            if(findBeltCardNum == threeShunNum && findThreeShunNum == threeShunNum){
                // 找到对应牌型了
                break;
            }
        }
        if(!isUseBomb || findBeltCardNum != threeShunNum || findThreeShunNum != threeShunNum){
            eatCards.cards = [];
            eatCards.num = 0;
        }
    }else if (mjMark.PLAY_CARD_TYPE_DOUBLE_SHUN == playCardType){
        // 双顺
        var doubleShunCard = 0;
        var doubleShunNum = playCards.num;
        var lastShunCard = 0;
        var findDoubleShunNum = 0;
        // 找到双顺最小牌
        for(var pos in playCards.cards){
            if(!playCards.cards.hasOwnProperty(pos)) {continue;}
            doubleShunCard = playCards.cards[ pos ].type;
        }
        for (var pos in playerHandsPokers.cards){
            if( !playerHandsPokers.cards.hasOwnProperty(pos) ){continue;}

            var iCard = playerHandsPokers.cards[pos];
            if(iCard.type == 2){
                continue;
            }
            if(!lastShunCard){
                lastThreeShunCard = iCard.type;
                ++findDoubleShunNum;
                continue;
            }
            if(lastShunCard + 1 != iCard.type || iCard.num != 2){
                lastThreeShunCard = 0;
                findDoubleShunNum = 0;
                continue;
            }
            lastThreeShunCard = iCard.type;
            ++findDoubleShunNum;
            eatCards.cards.push({type:iCard.type, num : iCard.num});
            eatCards.num += 1;
            if(findDoubleShunNum == doubleShunNum ) {
                break;
            }
        }
        if(findDoubleShunNum != doubleShunNum ){
            eatCards.cards = [];
            eatCards.num = 0;
        }
    }else if (mjMark.PLAY_CARD_TYPE_SINGLE_SHUN == playCardType) {

    }
    return eatCards;
}
*/

